'use strict';
const Controller = require('egg').Controller;

class MessageController extends Controller {
  // 查询所有的留言信息
  async index() {
    try {
      const { ctx, service } = this;
      const { page, pageSize } = this.ctx.query;
      const { count } = await this.app.model.Message.findAndCountAll();
      if (page && pageSize) {
        const MessageInfo = await service.messgae.getMessageList(page, pageSize);
        if (MessageInfo) {
          ctx.body = {
            code: 200,
            msg: '获取留言成功',
            data: MessageInfo,
            pageInfo: {
              page, // 查询起始位置
              pageSize,
            },
            total: count,
          };
        } else {
          ctx.body = {
            code: 500,
            msg: '留言获取失败',
          };
        }
      }

    } catch (error) {
      return error;
    }
  }
  // 根据id获取留言
  async show() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    const result = await service.messgae.getMessageById(id);
    // console.log(result);
    this.ctx.body = {
      data: result,
    };
  }
  // 新建留言信息
  async create() {
    const { ctx, service } = this;
    const messageData = ctx.request.body;
    const addMessList = await service.messgae.addMessage(messageData);
    if (addMessList) {
      ctx.body = {
        code: 200,
        msg: '留言成功',
        data: addMessList,
        reply: null,
      };
    }
  }
  // 删除留言信息
  // 根据id删除留言
  async destroy() {
    try {
      const { ctx, service } = this;
      const id = ctx.params.id;
      const del = await service.messgae.delMessage(id);
      if (del) {
        ctx.body = {
          code: 200,
          msg: '删除用户留言成功',
        };
      } else {
        ctx.body = {
          code: 400,
          msg: '删除留言失败',
        };
      }
    } catch (error) {
      this.ctx.logger.error(error);
      this.ctx.body = {
        code: error.code || 500,
        message: error.message || '服务器错误',
      };
    }
  }
  // 获取所有管理员的回复信息
  async getAdminReply() {
    const allAdminReply = await this.service.messgae.getReply();
    if (allAdminReply) {
      this.ctx.body = {
        code: 200,
        data: allAdminReply,
      };
    }
  }
  // 管理员对留言及进行回复
  async AdminReply() {
    try {
      const { ctx, service } = this;
      const id = ctx.params.id; // 评论的目标id
      const content = ctx.request.body.content;
      const reply = await service.messgae.replyUser(id, content);
      if (reply) {
        ctx.body = {
          code: 200,
          msg: '回复成功',
          data: reply,
        };
      } else {
        ctx.body = {
          code: 400,
          msg: '回复失败',
        };
      }
    } catch (error) {

      this.ctx.body = {
        code: 500,
        msg: this.ctx.logger.error(error),
      };
    }
  }
  // 管理员根据id修改回复
  async UpdateReply() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    const content = ctx.request.body.content;

    const updateReply = await service.messgae.updateRep(id, content);
    if (updateReply) {
      ctx.body = {
        code: 200,
        msg: '修改成功',
        data: updateReply,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '修改失败',
      };
    }
  }
}
module.exports = MessageController;
